package com.pdstudio.weather;

import com.pdstudio.app.Application;
import com.pdstudio.app.Application.EventHandler;
import com.pdstudio.bean.ArticleDetail;
import com.pdstudio.http.LiveHttpMethod;
import com.pdstudio.util.SharePreferenceUtil;
import com.pdstudio.view.SwipeBackActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class EmergencyActivity extends SwipeBackActivity implements
		EventHandler {

	private View customLiveIndexTitleView;
	private TextView txtTitle;
	private Handler handler = null;
	private TextView tvEmergencyTitle;
	private TextView tvEmergencyTime;
	private TextView tvEmergencyEntry;
	private ImageView ivBack;
	private SharePreferenceUtil mSpUtil;
	private Application mApplication;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_emergency);

		Application.mListeners.add(this);
		mApplication = Application.getInstance();
		mSpUtil = mApplication.getSharePreferenceUtil();

		LinearLayout layout = (LinearLayout) findViewById(R.id.activity_emergency);
		int resId = mApplication.getApplicationBackground(mSpUtil
				.getWeatherStatus());
		layout.setBackground(getResources().getDrawable(resId));

		customLiveIndexTitleView = findViewById(R.id.title_emergency);
		txtTitle = (TextView) customLiveIndexTitleView
				.findViewById(R.id.title2_text_nav);
		txtTitle.setText("应急响应");

		ivBack = (ImageView) customLiveIndexTitleView
				.findViewById(R.id.title2_back);
		ivBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				EmergencyActivity.this.finish();
			}

		});

		tvEmergencyTitle = (TextView) findViewById(R.id.emergency_title);
		tvEmergencyTime = (TextView) findViewById(R.id.emergency_posttime);
		tvEmergencyEntry = (TextView) findViewById(R.id.emergency_entry);
		// 设置textview可以滚动的代码
		tvEmergencyEntry.setMovementMethod(ScrollingMovementMethod
				.getInstance());

		// 开一条子线程加载网络数据
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(0);
					LiveHttpMethod http = new LiveHttpMethod(
							EmergencyActivity.this);
					ArticleDetail detail = new ArticleDetail();
					detail = http.GetEmergency(mSpUtil.getAuthCode(),
							mApplication.getCurSimpleCity().cityCode);
					Message msg = handler.obtainMessage(0, detail);
					handler.sendMessage(msg);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		};

		try {
			// 开启线程
			new Thread(runnable).start();
			// handler与线程之间的通信及数据处理
			handler = new Handler() {
				public void handleMessage(Message msg) {
					if (msg.what == 0) {

						// msg.obj是获取handler发送信息传来的数据
						@SuppressWarnings("unchecked")
						ArticleDetail detail = new ArticleDetail();
						// Toast.makeText(getApplicationContext(), "数据加载中", 0)
						// .show();
						if (msg != null && msg.obj != null) {
							detail = (ArticleDetail) msg.obj;

						}
						// 给ListView绑定数据
						setEmergency(detail);
					}
				}
			};
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void setEmergency(ArticleDetail detail) {
		try {
			if (detail != null && !detail.getTitle().equals("")) {
				tvEmergencyTitle.setText(detail.getTitle());
				tvEmergencyTime.setText(detail.getPostTime());
				tvEmergencyEntry.setText(detail.getContent());
			} else {
				tvEmergencyTitle.setText("目前无应急响应信息");
			}
		} catch (Exception e) {
			tvEmergencyTitle.setText("目前无应急响应信息");
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.emergency, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onCityComplite() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onNetChange() {
		// TODO Auto-generated method stub

	}
}
